package com.best.intelligentTrolley.serial_port

/**
 * author: shihao
 * date: 2021/4/9 15:43
 * description:
 */
object SerialPortCommand {
    /**
     * 数据包头
     */
    const val CMD_HEADER = "7E7E"

    /**
     * 设置主机地址为52
     */
    const val SETTING_ADDRESS_52 = "F152"

    /**
     * 设置主机地址为5A
     */
    const val SETTING_ADDRESS_5A = "F15A"

    /**
     * 设置主机地址为5D
     */
    const val SETTING_ADDRESS_5D = "F15D"

    /**
     * 设置主机地址为5E
     */
    const val SETTING_ADDRESS_5E = "F15E"

    /**
     * 开启透传
     */
    const val OPEN_TRANSPARENT_TRANSMISSION = "F211"

    /**
     * 关闭透传
     */
    const val CLOSE_TRANSPARENT_TRANSMISSION = "F210"

    /**
     * 获取红外信标id
     */
    const val GET_INFRARED_ID = CMD_HEADER + "FA01"

    /**
     * 查询温湿度以及过氧化氢浓度
     */
    const val QUERY_TEMPERATURE_HUMIDITY = CMD_HEADER + "A001"

    /**
     * 查询温湿度以及过氧化氢浓度
     */
    const val QUERY_PM25 = CMD_HEADER + "A0FF"

    /**
     * 查询设备版本号
     */
    const val QUERY_DEVICE_VERSION = CMD_HEADER + "57"

    /**
     * 串口通信回环测试
     */
    const val SERIAL_PORT_TEST = "F3000000"

    fun getDevCode(dev_code: String, flag: Int): String {
        return when (flag) {
            0 -> CMD_HEADER + "FB" + dev_code.substring(0, 14)
            1 -> CMD_HEADER + "FC" + dev_code.substring(14, 26)
            else -> CMD_HEADER
        }
    }

    /**
     * 便携干雾雾化片开关模块命令
     */
    val fogModuleCMD: FogModuleCMD = FogModuleCMD()

    class FogModuleCMD {

        private var ultrasonic1:String = "00"
        private var ultrasonic2:String = "00"

        /**
         * 雾化片1开关
         *  @param status 0关闭 1开启
         */
        fun setUltrasonic1Switch(status: Int): FogModuleCMD {
            ultrasonic1 = "0${status}"
            return this
        }

        /**
         * 雾化片2开关
         *  @param status 0关闭 1开启
         */
        fun setUltrasonic2Switch(status: Int): FogModuleCMD {
            ultrasonic2 = "0${status}"
            return this
        }

        fun build(): String {
            return CMD_HEADER + "C0E1" + ultrasonic1 + ultrasonic2
        }

    }


    /**
     * 便携干雾模块命令
     */
    val dryFogModuleCMD: DryFogModuleCMD = DryFogModuleCMD()

    /**
     * 便携干雾
     */
    class DryFogModuleCMD {
        private var indicatorLightCMD: String = "00"
        private var atomizationModeCMD:String = "00"
        private var atomizationRunSwitchCMD:String = "00"
        private var peristalticPumpSwitchCMD:String = "00"
        private var peristalticPumpSpeedCMD:String = "00"
        private var atomizationFanSpeedCMD:String = "00"

        /**
         * 设置指示灯板状态
         *  @param status 0不指示 1消毒中 2故障 3待机
         */
        fun setIndicatorLightStatusWithoutSwitch(status: Int): DryFogModuleCMD {
            indicatorLightCMD = "0${status}"
            return this
        }

        /**
         * 设置指示灯板状态
         *  @param status 0不指示 1消毒中 2故障 3待机
         */
        fun setIndicatorLightStatus(status: Int): DryFogModuleCMD {
            indicatorLightCMD = "0${status}"
            return this
        }

        /**
         * 设置雾化控制模式(便携干雾)
         *  @param status 0手动 1自动
         */
        fun setAtomizationMode(status: Int): DryFogModuleCMD {
            atomizationModeCMD = "0${status}"
            return this
        }

        /**
         * 设置雾化运行开关(便携干雾)
         *  @param status 0关闭 1启动
         */
        fun setAtomizationRunStatus(status: Int): DryFogModuleCMD {
            atomizationRunSwitchCMD = "0${status}"
            return this
        }

        /**
         * 设置蠕动泵开关(便携干雾)
         *  @param status 0关闭 1正转 2反转
         */
        fun setPeristalticPumpStatus(status: Int): DryFogModuleCMD {
            peristalticPumpSwitchCMD = "0$status"
            return this
        }

        /**
         * 设置蠕动泵速度(便携干雾)
         *  @param value 00关闭 0-100(百分比)
         */
        fun setPeristalticPumpSpeed(value: String): DryFogModuleCMD {
            peristalticPumpSpeedCMD = value
            return this
        }

        /**
         * 设置雾化风机速度(便携干雾)
         *  @param status 0关闭 0-100开启（速度百分比运行）
         */
        fun setAtomizationFanSpeed(value: String): DryFogModuleCMD {
            atomizationFanSpeedCMD = value
            return this
        }

        fun build(): String {
            return CMD_HEADER + "C0E0" + indicatorLightCMD + atomizationModeCMD + atomizationRunSwitchCMD + peristalticPumpSwitchCMD + peristalticPumpSpeedCMD + atomizationFanSpeedCMD
        }
    }

    /**
     * 指示灯板模块命令
     */
    val indicatorLightModuleCMD: IndicatorLightModuleCMD = IndicatorLightModuleCMD()

    /**
     * 指示灯板模块
     */
    class IndicatorLightModuleCMD {
        private var indicatorLightCMD: String = "00"


        fun build(): String {
            return CMD_HEADER + "C0" + indicatorLightCMD
        }
    }

    /**
     * 液位传感器模块
     */
    val liquidLevelModuleCMD: LiquidLevelModuleCMD = LiquidLevelModuleCMD()

    class LiquidLevelModuleCMD {

        /**
         * 查询液位数据
         */
        fun buildQueryLiquidLevel(): String {
            return CMD_HEADER + "F001"
        }

        /**
         * 查询液位最低最高容量
         */
        fun buildQueryLiquidLevelMinMax(): String {
            return CMD_HEADER + "F012"
        }


    }

    /**
     * 闪蒸器控制模块，控制闪蒸雾化控制，加热器控制，蠕动泵控制
     */
    val evaporatorModuleCMD: EvaporatorModuleCMD = EvaporatorModuleCMD()

    class EvaporatorModuleCMD {
        private var atomizeControl: String = "01"
        private var atomizeSwitch: String = "00"
        private var heatingSwitch: String = "00"
        private var pumpSwitch: String = "00"

        /**
         * 开启自动雾化
         */
        fun openAtomize(): EvaporatorModuleCMD {
            atomizeControl = "01"
            atomizeSwitch = "01"
            return this
        }

        /**
         * 关闭雾化
         */
        fun closeAtomize(): EvaporatorModuleCMD {
            atomizeControl = "01"
            atomizeSwitch = "00"
            return this
        }

        /**
         * 开启加热器开关
         */
        fun openHeating(): EvaporatorModuleCMD {
            atomizeControl = "00"
            heatingSwitch = "01"
            return this
        }

        /**
         * 关闭加热器
         */
        fun closeHeating(): EvaporatorModuleCMD {
            atomizeControl = "01"
            heatingSwitch = "00"
            return this
        }

        /**
         * 开启手动
         */
        fun openManual(): EvaporatorModuleCMD {
            atomizeControl = "00"
            return this
        }
        /**
         * 关闭手动
         */
        fun closeManual(): EvaporatorModuleCMD {
            atomizeControl = "01"
            return this
        }

        /**
         * 开启抽液蠕动泵
         * @param isDue true 正向，false 反向
         */
        fun openPump(isDue: Boolean): EvaporatorModuleCMD {
            //atomizeControl = "01"
            pumpSwitch = if (isDue) "01" else "02"
            return this
        }

        /**
         * 关闭抽液蠕动泵
         */
        fun closePump(): EvaporatorModuleCMD {
            //atomizeControl = "00"
            pumpSwitch = "00"
            return this
        }

        /**
         * 组装命令,0 第一个闪蒸器，1 第二个闪蒸器
         */
        fun build(index: Int): String {
            return CMD_HEADER + "B${index}" + atomizeControl + atomizeSwitch + heatingSwitch + pumpSwitch
        }

        /**
         * 修改闪蒸模块地址为 B0
         */
        fun buildSettingEvaporatorAddress(index: Int): String {
            return CMD_HEADER + "BCB${index}"
        }
    }

    /**
     * 超声款手动开启蠕动泵
     */
    val openPumpSD = CMD_HEADER + "B000000001"

    /**
     * 风机控制模块，控制左右扩散风机开关，解析风机开关
     */
    val fanModuleCMD: FanModuleCMD = FanModuleCMD()

    class FanModuleCMD {
        private var leftDiffusionFanFan: String = "00"
        private var rightDiffusionFanFan: String = "00"
        private var degradationFan: String = "00"

        /**
         * 关闭左扩散风机
         */
        fun closeLeftDiffusionFan(): FanModuleCMD {
            leftDiffusionFanFan = "00"
            return this
        }

        /**
         * 关闭右扩散风机
         */
        fun closeRightDiffusionFan(): FanModuleCMD {
            rightDiffusionFanFan = "00"
            return this
        }

        /**
         * 关闭扩散风机
         */
        fun closeDiffusionFan(): FanModuleCMD {
            leftDiffusionFanFan = "00"
            rightDiffusionFanFan = "00"
            return this
        }


        /**
         * 关闭解析风机
         */
        fun closeDegradationFan(): FanModuleCMD {
            degradationFan = "00"
            return this
        }

        /**
         * 开启解析风机
         */
        fun openDegradationFan(speed: String): FanModuleCMD {
            degradationFan = speed
            return this
        }

        /**
         * 组装风机控制模块命令
         */
        fun build(): String {
            return CMD_HEADER + "1001" + leftDiffusionFanFan + rightDiffusionFanFan + degradationFan
        }
    }

    /**
     * 主控模块，控制紫外线，等离子，机身旋转，闪蒸器电源的开关
     */
    val mainModuleCMD: MainModuleCMD = MainModuleCMD()

    class MainModuleCMD {
        private var uvCMD: String = "00"
        private var plasmaCMD: String = "00"
        private var deviceRotate: String = "00"
        private var evaporatorPower: String = "00"
        private var uadUVCMD: String = "00"
        private var lockCMD: String = "00"

        /**
         * 开启紫外线
         */
        fun openUltravioletRays(): MainModuleCMD {
            uvCMD = "02"
            return this
        }

        /**
         * 开启紫外线灯管旋转，灯管转向外
         */
        fun openUltravioletRaysTubeRotate(): MainModuleCMD {
            uvCMD = "01"
            return this
        }

        /**
         * 关闭紫外线
         */
        fun closeUltravioletRays(): MainModuleCMD {
            uvCMD = "00"
            return this
        }

        /**
         * 开启闪蒸器电源
         */
        fun openEvaporatorPower(): MainModuleCMD {
            evaporatorPower = "01"
            return this
        }

        /**
         * 关闭闪蒸器电源
         */
        fun closeEvaporatorPower(): MainModuleCMD {
            evaporatorPower = "00"
            return this
        }

        /**
         * 开启等离子
         */
        fun openPlasma(): MainModuleCMD {
            plasmaCMD = "01"
            return this
        }

        /**
         * 关闭等离子
         */
        fun closePlasma(): MainModuleCMD {
            plasmaCMD = "00"
            return this
        }

        /**
         * 开启设备自旋转
         */
        fun openDeviceRotate(): MainModuleCMD {
            deviceRotate = "01"
            return this
        }

        /**
         * 关闭设备自旋转
         */
        fun closeDeviceRotate(): MainModuleCMD {
            deviceRotate = "00"
            return this
        }

        /**
         * 开启升降紫外线
         */
        fun openUpAndDownUltravioletRays(): MainModuleCMD {
            uadUVCMD = "02"
            return this
        }

        /**
         * 开启升降紫外线机头升起，不开启紫外线
         */
        fun openUpAndDownUltravioletRaysOnlyUp(): MainModuleCMD {
            uadUVCMD = "01"
            return this
        }

        /**
         * 关闭升降紫外线
         */
        fun closeUpAndDownUltravioletRays(): MainModuleCMD {
            uadUVCMD = "00"
            return this
        }

        /**
         * 组装命令
         */
        fun build(): String {
            return CMD_HEADER + "1002" + uvCMD + plasmaCMD + deviceRotate + evaporatorPower
        }

        /**
         * 解锁
         */
        fun unlocking(): MainModuleCMD {
            lockCMD = "01"
            return this
        }

        /**
         * 锁定
         */
        fun locking(): MainModuleCMD {
            lockCMD = "00"
            return this
        }

        /**
         * 组装电子锁命令
         */
        fun buildLockCMD(): String {
            return CMD_HEADER + "1011" + lockCMD
        }
    }
}